// const nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3;

// var maxSlidingWindow = function (nums, k) {
//     const len = nums.length;
//     let left = 0
//     let right = k - 1
//     let arr = []
//     for (let i = 0; i <= len - k; i++) {
//         let max = -Infinity
//         for (let j = left; j <= right; j++) {
//             if (nums[j] > max) {
//                 max = nums[j]
//             }
//         }
//         left++
//         right++
//         arr.push(max)
//     }
//     return arr
// };
// console.log(maxSlidingWindow(nums, k));


const nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3;

var maxSlidingWindow = function (nums, k) {
    let arr = [] //递减两端队列
    let num = []
    for (let j = 0; j < nums.length; j++) {
        while (arr.length && nums[j] > nums[arr[arr.length - 1]]) {
            arr.pop()
        }
        arr.push(j)
        if (arr[0] <= j - k) {
            arr.shift()
        }
        if (j >= k - 1) {
            num.push(nums[arr[0]])
        }
    }
    return num
};
console.log(maxSlidingWindow(nums, k));

